package fun.easycode.jointblock.manager;

import com.google.common.collect.Lists;
import feign.hystrix.FallbackFactory;
import fun.easycode.jointblock.common.DapengConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@FeignClient(name = "ReadServiceClient",
    url = "http://api.cd0755d8e9df9425a8d1653d7ac3a863d.cn-beijing.alicontainer.com/article-api",
    fallbackFactory = ArticleServiceClient.ArticleServiceHystrix.class)
@Primary
public interface ArticleServiceClient {

  /**
   * 通过阅读IDS获取阅读列表
   * @param articleIds
   * @return
   */
  @GetMapping("/fore-articles/by-ids")
  ResponseEntity<List<ArticleClientDto>> getReadListByIds(@RequestParam String articleIds);

  /**
   * 通过阅读ID查询阅读详情
   * @param articleId
   * @return
   */
  @GetMapping("/fore-articles/{articleId}")
  ResponseEntity<ArticleClientDto> getReadById(@PathVariable String articleId);

  /**
   *  我的阅读 不带分页 广场区-阅读
   * @param articleClientQuery
   * @return
   */
  @GetMapping("/fore-articles")
  ResponseEntity<List<ArticleClientDto>> getReadList(@SpringQueryMap ArticleClientQuery articleClientQuery);

  /**
   *  我的阅读 带分页
   * @param articleClientQuery
   * @return
   */
  @GetMapping("/fore-articles/page-list")
  ResponseEntity<List<ArticleClientDto>> getRead(@SpringQueryMap ArticleClientQuery articleClientQuery);

  /**
   * 新增阅读
   * @param articleClientUpdateOrAdd
   * @return
   */
  @PostMapping("/fore-articles")
  ResponseEntity<ArticleBaseClientDto> addRead(@RequestBody ArticleClientUpdateOrAdd articleClientUpdateOrAdd);

  /**
   * 修改阅读
   * @param articleClientUpdateOrAdd
   * @return
   */
  @PutMapping("fore-articles/{articleId}")
  ResponseEntity<ArticleBaseClientDto> updateRead(
      @PathVariable String articleId,
      @RequestBody ArticleClientUpdateOrAdd articleClientUpdateOrAdd);

  @GetMapping("/fore-articles/by-ids")
  ResponseEntity<List<ArticleClientDto>> getArticleByIds(
      @RequestParam("articleIds") List<String> articleIds);

  /**
   * 删除阅读
   * @param articleId
   * @return
   */
  @PutMapping("/fore-articles/delete/{articleId}")
  ResponseEntity deleteRead(
      @RequestHeader(DapengConst.CURRENT_USER_ID_HEADER) String userId,
      @PathVariable String articleId);

  @Slf4j
  @Component
  class ArticleServiceHystrix implements FallbackFactory<ArticleServiceClient> {

    @Override
    public ArticleServiceClient create(Throwable throwable) {
      return new ArticleServiceClient(){

        @Override
        public ResponseEntity<List<ArticleClientDto>> getReadListByIds(String ids) {
          log.error("ReadServiceClient.getReadListByIds.触发熔断 --> ids:{},throw:{},error:{}",
              ids, throwable, throwable.getMessage());
          return ResponseEntity.ok(Lists.newArrayList());
        }

        @Override
        public ResponseEntity<ArticleClientDto> getReadById(String articleId) {
          log.error("ReadServiceClient.getReadById.触发熔断 --> articleId:{},throw:{},error:{}",
              articleId, throwable, throwable.getMessage());
          return ResponseEntity.ok(new ArticleClientDto());
        }

        @Override
        public ResponseEntity<List<ArticleClientDto>> getReadList(ArticleClientQuery articleClientQuery) {
          log.error("ReadServiceClient.getReadList.触发熔断 --> articleClientQuery:{},throw:{},error:{}",
              articleClientQuery, throwable, throwable.getMessage());
          return ResponseEntity.ok(Lists.newArrayList());
        }

        @Override
        public ResponseEntity<List<ArticleClientDto>> getRead(ArticleClientQuery articleClientQuery) {
          log.error("ReadServiceClient.getRead.触发熔断 --> ReadClientQuery:{},throw:{},error:{}",
              articleClientQuery, throwable,  throwable.getMessage());
          return ResponseEntity.ok(Lists.newArrayList());
        }

        @Override
        public ResponseEntity<ArticleBaseClientDto> addRead(
            ArticleClientUpdateOrAdd articleClientUpdateOrAdd) {
          log.error("ReadServiceClient.addRead.触发熔断 --> articleClientUpdateOrAdd:{}, throw:{}, error:{}",
              articleClientUpdateOrAdd, throwable, throwable.getMessage());
          return ResponseEntity.ok(new ArticleBaseClientDto());
        }

        @Override
        public ResponseEntity<ArticleBaseClientDto> updateRead(String articleId,
            ArticleClientUpdateOrAdd articleClientUpdateOrAdd) {
          log.error("ReadServiceClient.updateRead.触发熔断 --> articleId:{},articleClientUpdateOrAdd:{},"
              + "throw:{}, error:{}",articleId, articleClientUpdateOrAdd, throwable, throwable.getMessage());
          return ResponseEntity.ok(new ArticleBaseClientDto());
        }

        @Override
        public ResponseEntity deleteRead(String articleId,String userId) {
          log.error("ReadServiceClient.deleteRead.触发熔断 --> articleId:{},userId:{},throw:{},error:{}",
              articleId,userId,throwable,throwable.getMessage());
          return ResponseEntity.noContent().build();
        }

        @Override
        public ResponseEntity<List<ArticleClientDto>> getArticleByIds(List<String> ids) {
          log.error("ReadServiceClient.getArticleByIds.触发熔断 --> ids = {}, throw = {} ,error = {}",
              ids, throwable, throwable.getMessage());
          return ResponseEntity.ok(Lists.newArrayList());
        }
      };
    }
  }

}
